package com.jiazhong.springmvc.test01;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;


/**
 * @Auther:shuXin
 */
@Controller
@RequestMapping("request")
public class RequestController {
    //使用 Model 类型的形参
    public String testAttrRequestModel(Model model) {
        model.addAttribute("username", "zhangsan");
        return "ok";
    }

    //使用modelMap
    public String testAttrRequestModelMap(ModelMap modelMap) {
        modelMap.addAttribute("username", "zhangsan");
        return "ok";
    }

    //使用map集合
    public String testAttrRequestMap(Map<String, Object> map) {
        map.put("requestScopeMessageMap", "i am very happy[map]");
        return "ok";
    }

    public String testAttrOriginalRequest(HttpServletRequest request) {
        request.setAttribute("requestScopeMessageOriginal", "i am very happy[original]");
        return "ok";
    }

    //使用ModelAndView对象
    public ModelAndView testAttrByModelAndView() {
        //创建对象
        ModelAndView modelAndView = new ModelAndView();
        //赋值
        modelAndView.addObject("zhangsan");
        //给视图再来进行赋值
        modelAndView.setViewName("value");
        return modelAndView;
    }

    //session级别的共享域
    public String testAttrSession(HttpSession session){
        return "ok";
    }
}
